import {
    getDjRadioCatelist,
    getDjRadioRecommend,
    getDjRadios
} from "@/services/djradio";
import {
    CHANGE_RADIO_CATEGORY,
    CHANGE_CURRENT_ID,
    CHANGE_RECOMMENDS,
    CHANGE_RADIOS,
} from './constants';

const changeCategoryAction = (res) => ({
    type: CHANGE_RADIO_CATEGORY,
    categories: res.categories
})

const changeRecommendsAction = (res) => ({
    type: CHANGE_RECOMMENDS,
    recommends: res.djRadios
})

const changeRadiosAction = (res) => ({
    type: CHANGE_RADIOS,
    radios: res.djRadios
})

export const changeCurrentIdAction = (id) => ({
    type: CHANGE_CURRENT_ID,
    currentId: id
})

export const getRadioCategories = () => {
    return dispatch => {
        getDjRadioCatelist().then(res => {
            dispatch(changeCategoryAction(res));
            const currentId = res.categories[0].id;
            dispatch(changeCurrentIdAction(currentId));
        })
    }
}

export const getRadioRecommend = (currentId) => {
    return dispatch => {
        getDjRadioRecommend(currentId).then(res => {
            dispatch(changeRecommendsAction(res));
        })
    }
}

export const getRadios = (currentId, offset) => {
    return dispatch => {
        getDjRadios(currentId, 30, offset).then(res => {
            dispatch(changeRadiosAction(res));
        })
    }
}